<?php

class FauthComponent extends Object {
	var $userModel = null;
	var $action = null;
	var $user_id = null;
	var $allowActions = array();
	var $components = array('Session','Cookie','RequestHandler','Twitter');
	
	function initialize(&$controller,$settings = array() ){
		$controller->loadModel('User');
		$this->userModel = new User();
		$this->action = $controller->action;
		$this->user_id = $this->Session->check('user_id')  ? $this->Session->read('user_id') :  $this->Cookie->read('user_id');	
		$this->user_id = empty($_COOKIE['frexi_user_id']) == false ? $_COOKIE['frexi_user_id'] : $this->user_id;
	}
	
	// ログインしているかどうかを返す関数
	function isAuth(){
		return empty($this->user_id) ? false : true;	
	}
	
	function allow($actions = array()){
		$this->allowActions = am($this->allowActions , $actions);
	}
	
	function isAllowAction(){
		foreach ($this->allowActions as $key => $val) {
			if( $this->action == $val ){
				return true;
			}
		}
		return false;
	}
	
	// ユーザーIDを返す関数
	function user(){
		return $this->userModel->findById($this->user_id);
	}

	// ユーザーIDがあるときはindexに、ないときはログイン画面に遷移する関数	
	function canmove(){
		
		if(!$this->isAuth() && $this->action != 'login'){
			return true; //$this->redirect('../users/login/');
		}
		else{
			return false;
		}
	}
}
?>